package xhwl.retrofitrx;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import rx.Observable;

/**
 * Created by admin on 2017/1/12.
 */

public abstract class BaseApi {
//    public static final String BaseUrlYun = "https://weixin.yk2020.com/";
        public static final String BaseUrlYun = "http://192.168.100.228:8080/";

    //FBAEF125C40748F023F5CAEFB228CDB46915D4A0
    protected static String access_token = "";
    private static final int TimeOut = 10;
    /* 失败后retry次数*/
    private int retryCount = 0;
    /*失败后retry延迟*/
    private long retryDelay = 100;
    /*失败后retry叠加延迟*/
    private long retryIncreaseDelay = 10;
    /*是否显示网络错误*/
    private boolean showErrorMsg=true;

    protected static Retrofit retrofit;

    public static void setToken(String token) {
        access_token = token;
    }


    static {
        //手动创建一个OkHttpClient并设置超时时间缓存等设置
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(TimeOut, TimeUnit.SECONDS);
        builder.readTimeout(TimeOut, TimeUnit.SECONDS);
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(logging);
        builder.addInterceptor(new CustomInterceptor());
        /*创建retrofit对象*/
        retrofit = new Retrofit.Builder()
                .client(builder.build())
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(BaseUrlYun)
                .build();
    }

    public static class CustomInterceptor implements Interceptor {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            if(request.url().toString().contains(BaseUrlYun)){
                HttpUrl httpUrl = request.url().newBuilder()
                        .addQueryParameter("access_token", access_token)
                        .build();
                request = request.newBuilder().url(httpUrl).build();
            }
            return chain.proceed(request);
        }
    }

    private boolean showDialog = false;
    private boolean cancle = false;

    protected abstract Observable getObservable();

    public boolean isShowDialog() {
        return showDialog;
    }

    public void setShowDialog(boolean showDialog) {
        this.showDialog = showDialog;
    }

    public boolean isCancle() {
        return cancle;
    }

    public void setCancle(boolean cancle) {
        this.cancle = cancle;
    }

    public int getConnectionTimeOut() {
        return TimeOut;
    }

    public static int getTimeOut() {
        return TimeOut;
    }

    public int getRetryCount() {
        return retryCount;
    }

    public void setRetryCount(int retryCount) {
        this.retryCount = retryCount;
    }

    public long getRetryDelay() {
        return retryDelay;
    }

    public void setRetryDelay(long retryDelay) {
        this.retryDelay = retryDelay;
    }

    public long getRetryIncreaseDelay() {
        return retryIncreaseDelay;
    }

    public void setRetryIncreaseDelay(long retryIncreaseDelay) {
        this.retryIncreaseDelay = retryIncreaseDelay;
    }
    public void setShowErrorMsg(boolean show){
        this.showErrorMsg=show;
    }
    public boolean isShowErrorMsg(){
        return showErrorMsg;
    }
}
